<template>
  <a-form-model
    :ref="'lifestyleForm' + confirmType"
    :model="lifestyle"
    :label-col="formItemLayout.labelCol"
    :wrapper-col="formItemLayout.wrapperCol"
    labelAlign="left"
  >
    <a-form-model-item label="1.是否有饮酒史">
      <a-radio-group v-model="lifestyle.drinkingFrequency">
        <a-radio :value="1"> 无 </a-radio>
        <a-radio :value="2"> 有 </a-radio>
      </a-radio-group>
    </a-form-model-item>

    <a-form-model-item
      :label-col="{ span: 10 }"
      v-if="lifestyle.drinkingFrequency == 2"
      label="2.您平时一般多久喝一次酒"
    >
      <a-radio-group v-model="lifestyle.drinkingTimes">
        <a-radio :value="1"> 每天 </a-radio>
        <a-radio :value="2"> 一周2-3次 </a-radio>
        <a-radio :value="3"> 一周1次-3次 </a-radio>
        <a-radio :value="4"> 每月2-3次 </a-radio>
        <a-radio :value="4"> 其他 </a-radio>
      </a-radio-group>
    </a-form-model-item>

    <a-form-model-item
      :label-col="{ span: 10 }"
      v-if="lifestyle.drinkingFrequency == 2"
      label="3.多数情况下，每次您的啤酒饮量是？（注：一瓶 1000毫升）"
    >
      <a-radio-group v-model="lifestyle.drinkingBeer">
        <a-radio :value="1"> 1001-2000毫升 </a-radio>
        <a-radio :value="2"> 超过2000毫</a-radio>
        <a-radio :value="3"> 几乎不喝 </a-radio>
      </a-radio-group>
    </a-form-model-item>

    <a-form-model-item
      :label-col="{ span: 10 }"
      v-if="lifestyle.drinkingFrequency == 2"
      label="4.多数情况下，每次您的白酒饮量是？(注：约50毫升合1两)
"
    >
      <a-radio-group v-model="lifestyle.drinkingBaijiu">
        <a-radio :value="1"> 少于50毫升 </a-radio>
        <a-radio :value="2"> 50-100毫升</a-radio>
        <a-radio :value="3"> 101-250毫升 </a-radio>
        <a-radio :value="4"> 251-500毫升 </a-radio>
        <a-radio :value="5"> 超过500毫升 </a-radio>
        <a-radio :value="6"> 251-500毫升 </a-radio>
      </a-radio-group>
    </a-form-model-item>

    <a-form-model-item
      :label-col="{ span: 10 }"
      v-if="lifestyle.drinkingFrequency == 2"
      label="5.多数情况下，每次您的黄酒饮量是？(注：约50毫升合1两）"
    >
      <a-radio-group v-model="lifestyle.drinkingYellowWine">
        <a-radio :value="1"> 少于50毫升 </a-radio>
        <a-radio :value="2"> 50-100毫升</a-radio>
        <a-radio :value="3"> 101-250毫升 </a-radio>
        <a-radio :value="4"> 251-500毫升 </a-radio>
        <a-radio :value="5"> 超过500毫升 </a-radio>
        <a-radio :value="6"> 251-500毫升 </a-radio>
      </a-radio-group>
    </a-form-model-item>

    <a-form-model-item
      :label-col="{ span: 10 }"
      v-if="lifestyle.drinkingFrequency == 2"
      label="6.多数情况下，每次您的葡萄酒饮量是？(注：约50毫升合1两）"
    >
      <a-radio-group v-model="lifestyle.drinkingWine">
        <a-radio :value="1"> 少于50毫升 </a-radio>
        <a-radio :value="2"> 50-100毫升</a-radio>
        <a-radio :value="3"> 101-250毫升 </a-radio>
        <a-radio :value="4"> 251-500毫升 </a-radio>
        <a-radio :value="5"> 超过500毫升 </a-radio>
        <a-radio :value="6"> 251-500毫升 </a-radio>
      </a-radio-group>
    </a-form-model-item>

    <a-form-model-item label="饮酒情况评分">
      <a-rate v-model="lifestyle.drinkingHabitsScore"></a-rate>
      <span v-if="lifestyle.drinkingHabitsScore" class="ant-rate-text">{{ lifestyle.drinkingHabitsScore }}分</span>
    </a-form-model-item>
    <a-form-model-item label="小结">
      <a-textarea
        v-model="lifestyle.drinkingHabitsComment"
        :maxLength="100"
        :rows="4"
        placeholder="请输入100字以内的小结"
      />
      <div>
        <a :style="{ display: 'inline-block', float: 'right', color: 'red' }" @click="handleTemplateSave">
          保存到模版
        </a>
        <a
          @click="handleTemplate"
          :style="{ display: 'inline-block', float: 'right', marginRight: '20px', color: 'red' }"
        >
          从模版选择
        </a>
      </div>
    </a-form-model-item>
    <a-modal
      :visible="saveVisit"
      title="保存到模版"
      ref="saveVisit"
      width="500px"
      @ok="handleOk"
      @cancel="handleCancel"
      :confirm-loading="typeConfirmLoading"
    >
      <a-form-model ref="editform" :model="category" :rules="validatorRules">
        <a-form-model-item label="分类名称" :labelCol="{ span: 4 }" :wrapperCol="{ span: 8 }" prop="categoryType">
          <a-select placeholder="请选择分类" v-model="category.categoryType">
            <a-select-option v-for="item in list" :key="item.dataId" :value="item.dataId">{{
              item.categoryName
            }}</a-select-option>
          </a-select>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
    <advise-template ref="modalForm" @ok="modalFormOk"></advise-template>
    <!-- <template v-if="lifestyle.drinkingFrequency != 1">
      <a-form-model-item label="红酒" :style="{marginBottom: '5px'}">
        <a-input-number :min="1" :max="10000" v-model="lifestyle.redwine"/>
        <span class="ant-form-text">
          ml/次
        </span>
      </a-form-model-item>
      <a-form-model-item label="白酒" :style="{marginBottom: '5px'}">
        <a-input-number :min="1" :max="10000" v-model="lifestyle.whitewine"/>
        <span class="ant-form-text">
          ml/次
        </span>
      </a-form-model-item>
      <a-form-model-item label="啤酒" :style="{marginBottom: '5px'}">
        <a-input-number :min="1" :max="10000" v-model="lifestyle.yellowwine"/>
        <span class="ant-form-text">
          ml/次
        </span>
      </a-form-model-item>
    </template> -->
    <a-form-model-item :wrapper-col="{ span: 12, offset: 10 }">
      <a-button type="primary" @click="save" :loading="saveLoading">保存</a-button>
      <a-popconfirm placement="topLeft" ok-text="是" cancel-text="否" @confirm="confirm">
        <template slot="title">
          <p>此操作代表你已完成了页面的填写，是否继续？</p>
        </template>
        <a-button type="primary" style="margin-left: 30px" :loading="saveLoading">确认完成</a-button>
      </a-popconfirm>
    </a-form-model-item>
  </a-form-model>
</template>

<script>
import { postAction } from '@/api/manage'
import { mapGetters } from 'vuex'
import AdviseTemplate from '../../bloodProfile/modules/AdviseTemplate'
export default {
  name: 'Drinking',
  props: ['lifestyle', 'list'],
  components: { AdviseTemplate },
  data() {
    return {
      confirmType: 2,
      rate: 0,
      saveVisit: false,
      category: {},
      typeConfirmLoading: false,
      validatorRules: {
        categoryType: [{ required: true, message: '请选择分类' }]
      },
      formItemLayout: {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 2 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 20 }
        }
      },
      tailFormItemLayout: {
        wrapperCol: {
          xs: {
            span: 24,
            offset: 0
          },
          sm: {
            span: 16,
            offset: 2
          }
        }
      },
      saveLoading: false
    }
  },
  methods: {
    ...mapGetters(['orgCode']),
    save() {
      this.saveLoading = true
      const queryData = {
        dataId: this.lifestyle.dataId,
        drinkingBaijiu: this.lifestyle.drinkingBaijiu,
        drinkingBeer: this.lifestyle.drinkingBeer,
        drinkingFrequency: this.lifestyle.drinkingFrequency,
        drinkingHabitsComment: this.lifestyle.drinkingHabitsComment,
        drinkingHabitsScore: this.lifestyle.drinkingHabitsScore,
        drinkingTimes: this.lifestyle.drinkingTimes,
        drinkingWine: this.lifestyle.drinkingWine,
        drinkingYellowWine: this.lifestyle.drinkingYellowWine,
        patientId: this.lifestyle.patientId
      }
      postAction('/dossier/14/lifeway/drinking/update', queryData)
        .then(res => {
          if (res.success) {
            this.$message.success('操作成功')
            this.$emit('next', '3')
          } else {
            this.$message.error('操作失败')
          }
        })
        .finally(_ => {
          this.saveLoading = false
        })
    },
    handleOk() {
      this.$refs.editform.validate(valid => {
        if (valid) {
          const params = {
            patientId: this.lifestyle.patientId,
            signinType: this.category.categoryType,
            suggestContent: this.category.suggestContent,
            orgCode: this.orgCode()
          }
          postAction('/201/02/2010219', params).then(res => {
            if (res.success) {
              this.$message.success(res.message)
              this.handleCancel()
            } else {
              this.$message.warning(res.message)
            }
          })
        }
      })
    },
    handleCancel() {
      this.saveVisit = false
    },
    handleTemplateSave() {
      if (!this.lifestyle.drinkingHabitsComment) {
        this.$message.warning('请填写小结')
        return
      }
      this.category.suggestContent = this.lifestyle.drinkingHabitsComment
      this.saveVisit = true
    },
    modalFormOk(item) {
      this.lifestyle.drinkingHabitsComment = item
    },
    handleTemplate() {
      this.$refs.modalForm.add()
    },
    confirm() {
      this.saveLoading = true
      this.$emit('confirm', this.confirmType)
      this.$emit('next', '3')
    }
  }
}
</script>

<style lang="less" scoped></style>

